package com.jiaodacailei.ttv.md.util;

import com.jiaodacailei.ttv.md.domain.Description;
import com.jiaodacailei.ttv.md.domain.Dialogue;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DialogParser {

    public static void main(String[] args) {
        parseToFile("/Users/cailei/jp-learning/5/00.安全培训.md");
    }

    public static void parseToFile(String mdFilePath) {
        List<Dialogue> dialogueList = new ArrayList<>();
        List<Description> descriptionList = new ArrayList<>();

        try (BufferedReader br = new BufferedReader(new FileReader(mdFilePath))) {
            String line;
            Boolean wordByWord = false;
            while ((line = br.readLine()) != null) {
                // 使用正则表达式匹配Markdown文本中的场景对话格式
                Pattern dialoguePattern = Pattern.compile("(\\d+)\\.\\s+\\*\\*(.*)\\*\\*:\\s+(.*)");
                Matcher dialogueMatcher = dialoguePattern.matcher(line);

                if (dialogueMatcher.matches()) {
                    wordByWord = false;
                    Dialogue dialogue = new Dialogue();
                    dialogue.setNumber(Integer.parseInt(dialogueMatcher.group(1)));
                    dialogue.setName(dialogueMatcher.group(2));
                    dialogue.setJapanese(dialogueMatcher.group(3));
                    dialogueList.add(dialogue);
                } else if (line.indexOf("- **中文解释**:") > -1) {
                    wordByWord = false;
                    String chineseTranslation = line.split(": ")[1];
                    Dialogue dialogue = dialogueList.get(dialogueList.size() - 1);
                    dialogue.setChineseTranslation(chineseTranslation.trim());
                } else if (line.indexOf("- **逐字解释**:") > -1) {
                    wordByWord = true;
                } else if (wordByWord && line.trim().startsWith("- ")) {
                    String word = line.trim().substring(2);
                    Dialogue dialogue = dialogueList.get(dialogueList.size() - 1);
                    dialogue.addWord(word);
                } else {
                    wordByWord = false;
                    // 如果不匹配场景对话格式，将其视为说明性文字段落
                    Description description = new Description(line);
                    descriptionList.add(description);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        StringBuffer chinese = new StringBuffer();
        StringBuffer all = new StringBuffer();
        // 打印所有场景对话
        for (Dialogue dialogue : dialogueList) {
            String str = String.format("%02d. %s: %s\n", dialogue.getNumber(), dialogue.getName(), dialogue.getChineseTranslation());
            chinese.append(str);
            all.append(str);
            all.append("    日语文本: " + dialogue.getJapanese() + "\n");
            all.append("    逐字解释: " + dialogue.getWords() + "\n");
            all.append("\n");
            chinese.append("\n");
        }

        //
        File file = new File(mdFilePath);
        String dirOutput = file.getParent();
        String fileName = file.getName();
        outputToFile(chinese.toString(), String.format("%s/%s.中文.txt", dirOutput, fileName));
        outputToFile(all.toString(), String.format("%s/%s.中日文.txt", dirOutput, fileName));
        // 打印所有说明性文字段落
        for (Description description : descriptionList) {
            // System.out.println("说明性文字: " + description.getText());
        }
    }

    private static void outputToFile(String content, String filePath) {
        PrintWriter printWriter = null;
        try {
            // 创建FileWriter和BufferedWriter
            FileWriter fileWriter = new FileWriter(filePath);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

            // 使用PrintWriter将内容写入文件
            printWriter = new PrintWriter(bufferedWriter);
            printWriter.println(content);
            System.out.println("字符串已成功写入文件：" + filePath);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            printWriter.close();
        }
    }

}
